package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.util.R;
import com.github.pagehelper.Page;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @description 商品信息Controller
 */
@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询商品库存信息
     *
     * @param page        当前页
     * @param rows        每页显示条数
     * @param codeOrName  商品编码或名称
     * @param goodsTypeId 商品类别ID
     * @return
     */
    @PostMapping("/goods/listInventory")
    public R listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        Page<Goods> goods = goodsService.getListInventory(page, rows, codeOrName, goodsTypeId);
        return R.ok().put("rows",goods);
    }

    @PostMapping("/goods/list")
    public R list(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        Page<Goods> goods = goodsService.getList(page, rows, goodsName, goodsTypeId);
        return R.ok().put("rows", goods);
    }

    @PostMapping("/goods/save")
    public ServiceVO save(Goods goods, Integer goodsId) {
        if (goodsId == null) {
            goodsService.save(goods);
            return new ServiceVO(100, "添加成功");
        } else {
            goodsService.update(goods, goodsId);
            return new ServiceVO(100, "修改成功");
        }

    }

    @PostMapping("/goods/delete")
    public ServiceVO delete(Integer goodsId) {
        int i = goodsService.delete(goodsId);
        if (i > 0){
            return new ServiceVO(100, "删除成功");
        }
        return new ServiceVO(500, "删除失败");
    }

    @PostMapping("/goods/getNoInventoryQuantity")
    public R getNoInventoryQuantity(Integer page,Integer rows,String nameOrCode) {
        Page<Goods> page1 = goodsService.getNoInventoryQuantity(page,rows,nameOrCode);
        return R.ok().put("rows",page1);
    }

    @PostMapping("/goods/getHasInventoryQuantity")
    public R getHasInventoryQuantity(Integer page,Integer rows,String nameOrCode) {
        Page<Goods> page1 = goodsService.getHasInventoryQuantity(page,rows,nameOrCode);
        return R.ok().put("rows",page1);
    }

    @PostMapping("/goods/saveStock")
    public ServiceVO saveStock(Integer goodsId,Integer inventoryQuantity,double purchasingPrice) {
        goodsService.saveStock(goodsId,inventoryQuantity,purchasingPrice);
        return new ServiceVO(100,"添加成功");
    }


    @PostMapping("/goods/deleteStock")
    public ServiceVO deleteStock(Integer goodsId) {
        int i = goodsService.deleteStock(goodsId);
        if (i > 0){
            return new ServiceVO(100,"删除成功");
        }
        return new ServiceVO(500,"删除失败");
    }

    @PostMapping("/goods/listAlarm")
    public R listAlarm() {
        List<Goods> goods = goodsService.listAlarm();
        return R.ok().put("rows",goods);
    }
//    @PostMapping("/goodsType/loadGoodsType")
//    public String loadGoodsType(Integer page, Integer rows, String codeOrName, Integer goodsTypeId){
//        Map<String,Object> goods = goodsService.getListInventory(page,rows,codeOrName,goodsTypeId);
//        System.out.println(goods);
//        return null;
//    }

    /**
     * 分页查询商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param goodsName 商品名称
     * @param goodsTypeId 商品类别ID
     * @return
     */


    /**
     * 生成商品编码
     *
     * @return
     */
    @RequestMapping("/getCode")
    @RequiresPermissions(value = "商品管理")
    public ServiceVO getCode() {
        return goodsService.getCode();
    }

    /**
     * 添加或修改商品信息
     * @param goods 商品信息实体
     * @return
     */

    /**
     * 删除商品信息
     * @param goodsId 商品ID
     * @return
     */

    /**
     * 分页查询无库存商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */


    /**
     * 分页查询有库存商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */


    /**
     * 添加商品期初库存
     * @param goodsId 商品ID
     * @param inventoryQuantity 库存
     * @param purchasingPrice 成本价
     * @return
     */

    /**
     * 删除商品库存
     * @param goodsId 商品ID
     * @return
     */

    /**
     * 查询库存报警商品信息
     * @return
     */

}
